home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1993, 1994 Marc Parmet.
- * This file is part of the Macintosh port of GNU Emacs.
- *
- * GNU Emacs is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
-
- #if defined(THINK_C)
- #include <MacHeaders>
- #else
- #include <Types.h>
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <Windows.h>
- #include <Resources.h>
- #include <Errors.h>
- #endif
-
- #include <Palettes.h>
- #include <Folders.h>
- #include <Script.h>
-
- static int
- get_pref_file_spec(FSSpec *spec)
- {
- short err;
- short vRefNum;
- long parID;
- union {
- unsigned char **uc;
- Handle h;
- } name;
-
- err = FindFolder(kOnSystemDisk,'pref',kCreateFolder,&vRefNum,&parID);
- if (err) return err;
- name.h = GetResource('STR ',131);
- if (name.h == 0L) return resNotFound;
- HLock(name.h);
- err = FSMakeFSSpec(vRefNum,parID,*name.uc,spec);
- HUnlock(name.h);
- return err;
- }
-
- static int
- create_pref_file(FSSpec *spec,int old_refNum,short *new_refNum)
- {
- int i,err;
- Handle h;
- FInfo info;
- static struct copyres {
- long type;
- short src,dst;
- } copyres[] = {
- 'BNDL',129,128,
- 'FREF',132,132,
- 'EMAc',0,0,
- 'icl4',132,132,
- 'icl8',132,132,
- 'ICN#',132,132,
- 'ics#',132,132,
- 'ics4',132,132,
- 'ics8',132,132,
- 'STR ',132,-16397,
- 'vers',128,1,
- };
- int ncopyres = sizeof(copyres) / sizeof(struct copyres);
-
- FSpCreateResFile(spec,'EMAc','pref',smSystemScript);
- if (err = ResError()) return err;
- *new_refNum = FSpOpenResFile(spec,fsRdWrPerm);
- if (err = ResError()) goto error;
-
- // Give the pref file an icon and a message for the user when trying to open.
- for (i = 0; i<ncopyres; ++i) {
- UseResFile(old_refNum); if (err = ResError()) goto error;
- h = GetResource(copyres[i].type,copyres[i].src); if (h == 0L) { err = resNotFound; goto error; }
- DetachResource(h); if (err = ResError()) goto error;
- UseResFile(*new_refNum); if (err = ResError()) goto error;
- AddResource(h,copyres[i].type,copyres[i].dst,(ConstStr255Param)"");
- if (err = ResError()) goto error;
- }
-
- err = FSpGetFInfo(spec,&info);
- if (err) goto error;
- info.fdFlags |= fHasBundle;
- err = FSpSetFInfo(spec,&info);
- if (err) goto error;
- return noErr;
-
- error:
- FSpDelete(spec);
- return err;
- }
-
- // If pref is in file, set *h to a detached version of that resource.
- // Otherwise, *h is set to 0.
- int
- get_preference(int type,int n,Handle *h)
- {
- int err;
- short old_refNum,new_refNum;
- FSSpec spec;
-
- old_refNum = CurResFile();
- err = get_pref_file_spec(&spec);
- if (err) { *h = 0L; return err; }
- new_refNum = FSpOpenResFile(&spec,fsRdWrPerm);
- if (err = ResError()) { *h = 0L; return err; }
- if (type == 'pltt') {
- *h = (Handle)GetNewPalette(n);
- err = *h == 0L ? resNotFound : noErr;
- }
- else {
- *h = Get1Resource(type,n);
- if (*h != 0L) {
- DetachResource(*h);
- err = ResError();
- }
- else
- err = resNotFound;
- }
- CloseResFile(new_refNum);
- UseResFile(old_refNum);
- if (err) *h = 0L;
- return err;
- }
-
- // Set preference n to data in h. The caller
- // should not dispose of handle h after return.
- int
- set_preference(int type,int n,Handle h)
- {
- int err;
- short old_refNum,new_refNum;
- FSSpec spec;
- Handle k;
-
- old_refNum = CurResFile();
- err = get_pref_file_spec(&spec);
- switch (err) {
- case fnfErr:
- err = create_pref_file(&spec,old_refNum,&new_refNum);
- break;
- case noErr:
- new_refNum = FSpOpenResFile(&spec,fsRdWrPerm);
- err = ResError();
- break;
- default:
- return err;
- }
- if (err) return err;
-
- SetResLoad(0);
- k = Get1Resource(type,n);
- SetResLoad(1);
- if (k) RmveResource(k), DisposHandle(k);
-
- AddResource(h,type,n,(ConstStr255Param)"");
- if ((err = ResError()) != noErr) return err;
- CloseResFile(new_refNum);
- UseResFile(old_refNum);
- return noErr;
- }
-